'use strict';
app.controller('mindMapCtrl', ['$scope', '$modal', '$rootScope', '$http', '$filter', '$stateParams', '$timeout', 'lodash','$location', function ($scope, $modal, $rootScope, $http, $filter, $stateParams, $timeout, lodash,$location) {
    $rootScope.initBreadcrumb($scope.breadcrumb);
    //页面加载时，给headerElement设置元素
    $rootScope.initHeaderElements($scope.settings);
    var saveMindMap = {
        show: true,
        label: "保存",
        func: function () {
            saveMindMapData();
        }
    }
    var backUrl = {
        show: true,
        label: "返回",
        func: function () {
            $rootScope.backHistory();
        }
    }
    $scope.settings.btnEle.push(saveMindMap);
    $scope.settings.btnEle.push(backUrl);

    $scope.info = {
        mindMapData:  {},
        diagramScope: {},
    };

    getReqName();
    getMindData();

    //获取req名称
    function getReqName() {
        var url = matrix_product_url +"/brdRequirement/getBrdRequirements?withFeatureDesc=0&reqId="+ $stateParams.reqId;
        $http.get(url).success(function(result) {
            if(result.code == "0") {
                if(result.data) {
                    $scope.info.reqDetails = result.data[0];
                    $scope.breadcrumb.push('产品设计', $scope.info.reqDetails ? $scope.info.reqDetails.reqName : "");
                }
            } else {
                $rootScope.toasterWarningInfo(result.desc);
            }
        });
    }

    // 获取思维导图数据
    function getMindData() {
        var url = matrix_product_url +"/brdRequirement/getGuideMap?reqId="+ $stateParams.reqId;
        $http.get(url).success(function(result) {
            if(result.code == "0") {
                var defaultNodeData = [
                    {"key":0, "text":"思维导图", "loc":"0 0"},
                    {"key":1, "parent":0, "text":"场景设计", "brush":"skyblue", "dir":"right", "loc":"95 -20.7560791015625"},
                    {"key":2, "parent":0, "text":"功能点设计", "brush":"darkseagreen", "dir":"right", "loc":"85 32.7560791015625"}];
                if(!result.data) {
                    $scope.info.mindMapData = {
                        "class": "go.TreeModel",
                        "nodeDataArray": defaultNodeData
                    };
                    return false;
                }
                var nodeDataArray = JSON.parse(result.data.nodeDataArray) ? JSON.parse(result.data.nodeDataArray) : defaultNodeData;
                $scope.info.mindMapData = {
                    "class": result.data.class,
                    "nodeDataArray": JSON.parse(result.data.nodeDataArray)
                };
            } else {
                
                $rootScope.toasterErrorInfo("数据获取异常！");
            }
        })
    }

    // 保存思维导图数据
    function saveMindMapData() {
        $scope.info.diagramScope.getMindData();
        if(!$stateParams.reqId) {
            $rootScope.toasterWarningInfo("reqid不能为空！");
            return false;
        }
        var paramsData = {
            "class": "go.TreeModel",
            "nodeDataArray": $scope.info.mindMapData.nodeDataArray
        }
        var url = matrix_product_url +"/brdRequirement/saveBrdRequirementGuideMap?reqId="+ $stateParams.reqId;
        $http({
            method: "post",
            url: url,
            data: JSON.stringify(paramsData),
            headers: {"Content-Type": "application/json"}
        }).success(function(result) {
            if(result.code == "0") {
                $rootScope.toasterSuccessInfo("保存成功！");
            } else {
                $rootScope.toasterErrorInfo(result.desc);
            }
        })
    }
}]);